package cn.springstudio.springui.template;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import cn.springstudio.springui.R;

/**
 * 名称：<b>LayoutDialog</b>
 * <p>作者：Spring Studio</p>
 * <p>版本：1.0</p>
 * <p>日期：2022/02/25 17:14</p>
 * <p>说明：</p>
 */
public class LayoutDialog extends DialogFragment {

    @LayoutRes
    private int resource;
    ViewListener listener;

    protected LayoutDialog(@LayoutRes int resource, ViewListener listener) {
        this.resource = resource;
        this.listener = listener;
    }

    public static LayoutDialog newInstance(@LayoutRes int resource) {
        return new LayoutDialog(resource, null);
    }

    public static LayoutDialog newInstance(@LayoutRes int resource, ViewListener listener) {
        return new LayoutDialog(resource, listener);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogFullScreen);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(resource, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        if (listener != null) {
            listener.bindView(view);
        }
    }

    public LayoutDialog setViewListener(ViewListener listener) {
        this.listener = listener;
        return this;
    }

    public void show(@NonNull FragmentManager manager) {
        super.show(manager, getClass().getName() + resource);
    }
}
